﻿using MySql.Data.MySqlClient;
using System;
using System.Data.Common;
using XuanFe.Database.Common;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.Database.BLL
{
    public class StaffLogBLL : AbstractBase
    {
        private static StaffLogBLL instance = new StaffLogBLL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static StaffLogBLL Instance
        {
            get
            {
                return instance;
            }
        }
        public StaffLogBLL() : base() { }
        public void InsertLog(string staffId, string moduleId, string remark)
        {
            decimal seq = SequenceBLL.Instance.GetSeqNextValue("WS_LOG_ID_SEQ");
            string id = string.Concat(DateTime.Now.ToString("yyyyMMdd"), Convert.ToString(seq).PadLeft(8, '0'));
            StaffLogModel model = new StaffLogModel() { Id = id, StaffId = staffId, OperateTime = DateTime.Now, ModuleId = moduleId, Remark = remark };

            using (DbConnection db = MySQLHelper.Instance.CreateConnection())
            {
                try
                {
                    db.Open();

                    using (DbTransaction tran = db.BeginTransaction())
                    {
                        try
                        {
                            StaffLogDAL.Instance.Insert(tran, model);

                            tran.Commit();
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            if (this.Logger.IsErrorEnabled)
                            {
                                this.Logger.Error("插入日志发生异常", ex);
                            }
                        }
                    }
                }
                finally
                {
                    db.Close();
                }
            }
        }
    }
}
